package com.woniuxy.user.service.impl;
import java.time.LocalDateTime;
import java.util.Date;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.user.dto.AddUserDto;
import com.woniuxy.user.dto.SystemUserDto;
import com.woniuxy.user.entity.SystemUser;
import com.woniuxy.user.mapper.SystemUserMapper;
import com.woniuxy.user.service.ISystemUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lzj
 * @since 2023-11-07
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser> implements ISystemUserService {

    @Autowired
    private SystemUserMapper mapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public SystemUserDto getUserByPhone(String userPhone) {

        long startTime = System.currentTimeMillis(); // 记录开始时间（毫秒）
        SystemUser user = mapper.selectOne(Wrappers.lambdaQuery(SystemUser.class)
                .select(SystemUser::getUserPhone).eq(SystemUser::getUserPhone, userPhone));
        long endTime = System.currentTimeMillis(); // 记录查询结束时间
        long duration = endTime - startTime; // 计算查询耗时
        String execuTime = duration + "ms";
        SystemUserDto systemUserDto = new SystemUserDto();
        systemUserDto.setUserPhone(user.getUserPhone());
        systemUserDto.setExecutionTime(execuTime);

        return systemUserDto;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public int batchDelete(List<Integer> ids) {
//        return mapper.batchDeleteByIds(ids);
        return mapper.deleteBatchIds(ids);
    }

    /**
     * 根据性别查询分页
     * @param userSex
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<SystemUser> getByUserSex(Integer userSex, Integer pageNum, Integer pageSize) {
        Page<SystemUser> page = Page.of(pageNum, pageSize);
        LambdaQueryWrapper<SystemUser> wrapper = Wrappers.lambdaQuery(SystemUser.class)
                .eq(userSex != null,SystemUser::getUserSex, userSex)
                .orderByDesc(SystemUser::getCreateTime);
        Page<SystemUser> pageData = this.page(page, wrapper);
        return pageData;
    }

    @Override
    public void addUser(AddUserDto addUserDto) {
        SystemUser systemUser = new SystemUser();
        systemUser.setUserUuid(RandomUtil.randomString(20));
        systemUser.setUserName(addUserDto.getUserName());
        systemUser.setUserPassword(passwordEncoder.encode("123456"));
        systemUser.setUserPhone(addUserDto.getUserPhone());
        systemUser.setUserAddress(addUserDto.getUserAddress());
        systemUser.setUserStatus(addUserDto.getUserStatus());
        systemUser.setUserSex(addUserDto.getUserSex());
        systemUser.setCreateTime(LocalDateTime.now());
        baseMapper.insert(systemUser);
    }

    @Override
    public Page<SystemUser> getByUserName(String userName, Integer pageNum, Integer pageSize) {
        Page<SystemUser> page = Page.of(pageNum, pageSize);
        LambdaQueryWrapper<SystemUser> wrapper = Wrappers.lambdaQuery(SystemUser.class)
                .likeRight(userName != null,SystemUser::getUserName, userName)
                .orderByDesc(SystemUser::getCreateTime);
        Page<SystemUser> pageData = this.page(page, wrapper);
        return pageData;
    }

}
